layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#sRescManageList',
        url : '/manage/sRescManage/data',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 15,
        limits : [10,15,20,25],
        id : "sRescManageListTable",
        cols : [[
            {field: 'RESC_ID', title: '资源ID', align:"left"},
            {field: 'RESC_NAME', title: '资源名称', align:"left"},
            {field: 'ORDER_ID', title: '序号', align:'left'},
            {field: 'USE_FLAG', title: '是否启用', align:'left',templet:function(d){
                    if(d.USE_FLAG == 'Y'){return '是';}
                    else if(d.USE_FLAG == 'N'){return '否';}
                }},
            {title: '操作', width:170, templet:'#sRescManageListBar',fixed:"right",align:"center"}
        ]]
    });


    //添加文章
    function add(){

        var index = layui.layer.open({
            title: "添加导航菜单",
            type: 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["550px", "400px"],
            content : "/manage/sRescManage/add",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })

    }

    function update(data){
        var index = layui.layer.open({
            title : "修改资源菜单",
            type : 2,
            shadeClose: false,           //弹出框之外的地方是否可以点击
            area: ["550px", "450px"],
            content : "/manage/sRescManage/update?rescId="+data.RESC_ID,
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        })
    }
    $(".add_btn").click(function(){
        add();
    })

    //列表操作
    table.on('tool(sRescManageList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            update(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('删除后将导致系统运行不稳定，确定删除此菜单资源？',{icon:3, title:'提示信息'},function(index){
                $.post("/manage/sRescManage/delete",{
                    rescId : data.RESC_ID
                },function(data){
                    data = JSON.parse(data)
                    if(data.flag ==="success"){
                        layer.msg("删除成功", {icon: 1});
                        tableIns.reload();
                        layer.close(index);
                    }else if(data.flag ==="exsit"){
                        layer.msg("该资源下面存在菜单配置，无法删除！", {icon: 4});
                    }else{
                        layer.msg("删除失败！", {icon: 2});
                    }
                })
            });
        }
    });

})